package com.b1502.store2.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.b1502.store2.R;
import com.b1502.store2.model.CategoryItem;
import com.b1502.store2.util.ImageLoaderUtils;
import com.b1502.store2.util.UrlUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * 作者：朱振江 on 2017/2/27 0027 20:20
 * 邮箱：zzjdys@Gmail.com
 * 类用途:
 */


public class CategoryItemAdapter extends BaseAdapter {
    private Context mContext;
    private CategoryItem[] mList;
    private final ImageLoader mImageLoader;
    private final DisplayImageOptions mOptions;


    public CategoryItemAdapter(Context context) {
        mContext = context;
        mImageLoader = ImageLoader.getInstance();
        mOptions = ImageLoaderUtils.initOptions();
    }

    public void setData(CategoryItem[] list) {
        mList = list;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return 10;
    }

    @Override
    public Object getItem(int position) {
        return mList[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        viewhord  v;
        if (convertView==null)
        {
         v=new viewhord();
         convertView=View.inflate(mContext, R.layout.fragment_category_gridviewitem,null);   
            v.mImageView= (ImageView) convertView.findViewById(R.id.category_grid_image);
            v.mTextView= (TextView) convertView.findViewById(R.id.category_grid_name);
            convertView.setTag(v);
        }else 
        {
         v= (viewhord) convertView.getTag();   
        }

        String imagePath = mList[position].getImagePath();
        String imageUrl = UrlUtil.getImageUrl(imagePath);

        mImageLoader.displayImage(imageUrl,v.mImageView,mOptions);
        v.mTextView.setText(mList[position].getName());
        
        return convertView;
    }
    
    class viewhord
    {
        ImageView mImageView;
        TextView mTextView;
    }
    
}